import CardScanner from '../../../utils/Component/cardscanner/cardscanner.js'
var common = require('../../../utils/common.js')
var context = null;// 使用 wx.createContext 获取绘图上下文 context
var isButtonDown = false;
var arrx = [];
var arry = [];
var arrz = [];
var canvasw = 0;
var canvash = 0;

Page({
  data: {
    handinput: [],
    fg: 0,
    page: 0,//总共
    pagenum: 0,//当前
    future: 0,
    hahaha:0,
    id:0
  },
  canvasIdErrorCallback: function (e) {
    console.error(e.detail.errMsg)
  },
  canvasStart: function (event) {

    isButtonDown = true;
    arrz.push(0);
    arrx.push(event.changedTouches[0].x);
    arry.push(event.changedTouches[0].y);
    //context.moveTo(event.changedTouches[0].x, event.changedTouches[0].y);

  },
  //画图
  canvasMove: function (event) {
   
    if (isButtonDown) {
      // console.log('a')    
      
      arrz.push(1);
      arrx.push(event.changedTouches[0].x);
      arry.push(event.changedTouches[0].y);
    };

    for (var i = 0; i < arrx.length; i++) {
      if (arrz[i] == 0) {
        context.moveTo(arrx[i], arry[i])
      } else {
        context.lineTo(arrx[i], arry[i])
      };

    };
    context.clearRect(0, 0, canvasw, canvash);
    context.stroke();

    context.draw(true);
  },
  canvasEnd: function (event) {
    isButtonDown = false;
  },
  //清除画布
  cleardraw: function () {
    arrx = [];
    arry = [];
    arrz = [];
    context.clearRect(0, 0, 50000000, 500000000);
    context.draw();
  },
  tianjia: function () {

    this.bindConfirm();
    var pagenum = this.data.pagenum;//当前页
    var page = this.data.page;//总页数
    var future = this.data.future;
    
    page = page + 1;
    future = page;
    this.setData({
      hahaha:1,
      page: page,
      pagenum: pagenum,
      future: future
    });
    // console.log(this.data.handinput);
    //清除画布
    this.cleardraw();
  }, 
  shang() {
    let pagenum = this.data.pagenum;
    console.log(pagenum);
    if (pagenum <= 0) {
      common.showToast("没有上一页", "none");
    } else {

      this.bindConfirm();
      this.cleardraw();
      let future = this.data.pagenum;
      future = future-1;
      // let pagenum=this.data.pagenum;
      // pagenum=pagenum-1;
      console.log(future);
      this.setData({
        // pagenum:pagenum,
        future: future,
      });

      
      


    }
  },
  xia() {
    let mian = this.data.handinput;
    let page = this.data.page;
    if (this.data.pagenum < this.data.page) {

      this.bindConfirm();
      this.huatu();


      this.cleardraw();
      let future = this.data.pagenum;
      future = future + 1;
      console.log(future);
      this.setData({
        future: future,
      });
      
      // this.setData({
      //   pagenum: 1,
      // });



    } else {
      common.showToast("没有下一页", "none");
    }

  },

  onLoad(options) {
    console.log(options);
    this.setData({
      id: options.id
    })

    context = wx.createCanvasContext('scannerCanvas');
    context.beginPath()
    context.setStrokeStyle('#000000');
    context.setLineWidth(4);
    context.setLineCap('round');
    context.setLineJoin('round');
    let that = this
    this.imgPath = options.imgPath
    this.cardScanner = new CardScanner(this)
      .on('ImageChanged', (imgPath) => {
        that.imgPath = imgPath
        console.log(imgPath)
      })
      .on('DecodeStart', (imgPath) => {
        wx.showLoading({
          title: '解析中',
          mask: true
        })
      })
      .on('DecodeComplete', (res) => {
        if (res.code == 0) {

          var handinput = this.data.handinput;
          var pagenum = this.data.pagenum;//当前页
          console.log(pagenum);
          handinput[pagenum] = res.data;
          var future = this.data.future;
          pagenum = future;
          console.log(pagenum);
          this.setData({
            pagenum: pagenum,
            handinput: handinput,
          });
          console.log(this.data.handinput);
          let hahaha=this.data.hahaha;
          if(hahaha==1){
            this.setData({
              hahaha:0
            });
          } else if(hahaha == 3){
            this.fanhui();
          }else{
            this.huatu();
          }
          // var cj = this.data.handinput;
          // var page=this.data.page
          // var pagenum = this.data.pagenum;
          // var zuipage=parseInt(page) + parseInt(pagenum);

          // cj[pagenum] = res.data;
          // // console.log(cj+'cj');
          // this.setData({
          //   handinput: cj,
          //   page: zuipage
          // });


          // console.log(this.data.handinput);

        } else {
          console.log('解析失败：' + res.reason)
        }
        wx.hideLoading()
      })
  },

  huatu() {

    var pagenum = this.data.pagenum;
    console.log(pagenum);
    let handinput = this.data.handinput;
    var src = "data:image/png;base64," + handinput[pagenum];
    console.log(src);
    context.drawImage(src, 0, 0, 500, 500);
    context.draw();
  },
  chakan(){
    

    this.bindConfirm();
    this.cleardraw();
    let future = this.data.pagenum;
    future = future;
    console.log(future);
    this.setData({
      // pagenum:pagenum,
      future: future,
      hahaha:3
    });


    
  },
  fanhui(){

    var handinput = this.data.handinput;
    var handinputs = "";
    for (let a = 0; a < handinput.length; a++) {
      if (a == 0) {
        handinputs = handinput[a];
      } else {
        handinputs = handinputs + "||||" + handinput[a];
      }
    }

    var shouxie = common.getStorageSync("shouxie");
    let id = parseInt(this.data.id);
    console.log(handinputs);
    shouxie[id] = handinputs;
    console.log(shouxie);

    common.setStorage('shouxie', shouxie);
    wx.navigateBack({ changed: true });//返回上一页  

  },
  onReady() {
    this.cardScanner.setImage(this.imgPath)
  }
})